from pynput import keyboard
import tkinter as tk
import os
import threading
import requests

CONFIG_FILE = os.path.join(os.path.expanduser('~'), "translate.txt")
target_url = ""
try:
    with open(CONFIG_FILE, "r") as f:
        target_url = f.readline().strip()
except FileNotFoundError:
    pass

window = tk.Tk()
text = tk.Text(window, height=1)
text.insert("1.0", target_url)
text.pack()


def on_savebutton_click():
    t = text.get("1.0", "end-1c")
    with open(CONFIG_FILE, "w") as f:
        f.write(t)


button = tk.Button(window, text="修改配置", command=on_savebutton_click)
button.pack()

prev2_content = ""
prev1_content = ""
now_content = ""


def on_activate_i():
    global prev2_content
    global prev1_content
    global now_content
    clipboard_content = tk.Tk().clipboard_get()
    prev2_content = prev1_content.strip()
    prev1_content = now_content.strip()
    now_content = clipboard_content.strip()

    if now_content == prev1_content and now_content == prev2_content:
        requests.get(target_url, params={"entry": now_content})


class HotKeyThread(threading.Thread):
    def run(self):
        with keyboard.GlobalHotKeys({'<ctrl>+c': on_activate_i}) as h:
            h.join()


hotKeyThread = HotKeyThread()
hotKeyThread.start()
window.mainloop()
hotKeyThread.join()
